/**
 * 企业管理--js
 */
var enterprise_manage = {
    MODULE_ID : "",
    Table_Obj : null,// 表格对象
    USER_:null,
    /**
     * 初始化，包括请求一次搜索数据，保存MODULE_ID
     */
    init : function() {
    	enterprise_manage.USER_= getSessoin();
        enterprise_manage.MODULE_ID = GetRequest().pid;
        enterprise_manage.loadData();
    },
    /**
     * 请求搜索数据
     */
    loadData : function() {
        var obj = enterprise_manage.getParam();
        $.ajax({
            url : "/crm/retrieval/comSearch", // 用solr检索
            dataType : "json", // 返回格式为json
            data : obj, // 参数值
            type : "post", // 请求方式
            success : function(data) {
                enterprise_manage.initTable(data.rows);
                $("#total_records").text(data.total);
                $("#this_page").text(data.currentPage);
                $("#total_page").text(data.totalPages);
                var url = "/crm/retrieval/comSearch";
                setCommon(obj, Table_Obj, url);
            },
            error : function() {
            }
        })
    },
    /**
     * 检索获取条件
     */
    getParam : function() {
        var obj = {};
        obj.pageIndex = 1;
        obj.pageSize = $("#page_size").val();
        obj.orderBy = "create_time";
        obj.orderDesc = 1;
        obj.table = "data_enterprise";
        obj.text = $("#searchWords").val().replace(/ /g, '@#@');
        obj.higthText="AND,ent_stats,0,0";
        return obj;
    },
    /**
     * 初始化表格
     */
    initTable : function(datas) {
        Table_Obj = $("#TableContainer").easyTable({
            data : datas, // 初始数据，动态添加可以通过setData
            hideCheckbox : false, // 否显示复选框，获取复选框选择的数据用方法getCheckedItem
            clickChangeColor:false,//单击之后切换颜色，
			doubleClickChangeColor:true,//双击之后切换颜色，
            // rowClick : function(data) {// 行点击回调，参数为改行数据
            // alert(JSON.stringify(data))
            // },
            rowDoubleClick : function(data) {// 行双击回调，参数为改行数据
                enterprise_manage.lookDetail(data.uuid);
            },
            columns : [ {
                title : "名称",
                field : "ent_name"
            }, {
                title : "行业大类",
                field : "ent_industry_one",
                render : function (data) {
                    if(!isBlank(data.ent_industry_one)) {
                        try {
                            return JSON.parse(data.ent_industry_one).indName
                        } catch(e) {
                            console.log("转换行业大类失败");
                        }
                    }
                    return "";

                }
            }, {
                title : "行业子类",
                field : "ent_industry_two"
            }, {
                title : "上游企业",
                field : "ent_upstream"
            }, {
                title : "下游企业",
                field : "ent_downstream"
            }, {
                title : "紧急需求",
                field : "ent_needs_urgent",
                render : function (data) {
                    if(!isBlank(data.ent_needs_urgent)) {
                        try {
                            return JSON.parse(data.ent_needs_urgent).join(",")
                        } catch(e) {
                            console.log("转换紧急需求失败");
                        }
                    }
                    return "";
                }
            }, {
                title : "一般需求",
                field : "ent_needs_commonly",
                render : function (data) {
                    if(!isBlank(data.ent_needs_commonly)) {
                        try {
                            return JSON.parse(data.ent_needs_commonly).join(",")
                        } catch(e) {
                            console.log("转换一般需求失败");
                        }
                    }
                    return "";
                }
            }, {
                title : "创建时间",
                field : "create_time"
            }, {
                title : "上次编辑",
                field : "last_edit_time"
            }, {
                title : "操作",
                render : function(data) {
                    return enterprise_manage.loadOperator(data);
                }
            }]
        });
    },
    /**
     * 加载方法
     *
     * @param data
     * @returns {String}
     */
    loadOperator : function(data) {
        var html = "";
        html += '<a onclick = "enterprise_manage.lookDetail(\'' + data.uuid + '\',this)">查看</a>';
        var str = data.edit_power_user_uuids.indexOf(enterprise_manage.USER_.uuid);
        if(str!= -1||(data.liable_user_uuid==enterprise_manage.USER_.uuid)||(enterprise_manage.USER_.uuid=="admin")){
			html += '<a class="ml10" onclick = "enterprise_manage.edit(' + data.id + ')">编辑</a>';
		}
        // html += '<a class="ml10" onclick = "enterprise_manage.deleteThis(' + data.id + ')">删除</a>';
        return html;
    },
    /**
     * 检索
     */
    searchData : function() {
        var pageSize = $("#page_size").val();
        var pageIndex = 1;
        var obj = {};
        obj.pageSize = pageSize;
        obj.pageIndex = pageIndex;
        var other = enterprise_manage.getParam();
        reset_serach(other, obj);
    },
    /**
     * 创建企业
     */
    addSave : function(){
        parent.createPage("创建企业", "../html/enterprise/enterprise_add.html", true, "enterprise_add");
    },
    /**
     * 查看详情
     * @param uuid uuid
     */
    lookDetail : function(uuid,_this) {
    	$(_this).parent().parent("tr").css("color", "red");
        parent.createPage("企业详情", "../html/enterprise/enterprise_detail.html?uuid=" + uuid, true, "enterprise_detail");
    },
    /**
     * 编辑企业
     */
    edit : function(id){
        parent.createPage("编辑企业", "../html/enterprise/enterprise_edit.html?id=" + id, true, "enterprise_edit");
    },
    /**
     * 删除操作，已废弃，当前页面不使用
     */
    deleteThis : function(id) {
        layer.confirm('确定要删除吗?', {btn : ['确定', '取消'], title:"提示"}, function(){
            layer.confirm('再次确认', {btn : ['确定', '取消'], title:"提示"}, function() {
                $.ajax({
                    type: "post",
                    url: "/crm/enterprise/delete",
                    data: {
                        ids:id
                    },
                    dataType: "json",
                    async:false,
                    success:function(data) {
                        if(data.status){
                            layer.alert(data.info+"请稍后刷新查看！", {icon: 1},function(index){
                                enterprise_manage.searchData();
                                layer.close(index);
                            });

                        }else{
                            layer.msg(data.info, {icon: 2});
                        }
                    }
                });
            });
        });
    },
    /**
     * 多项删除企业
     */
    batchDelete : function() {
        var data = Table_Obj.getCheckedItem();
        if (data.length === 0) {
            layer.msg("未选择有效数据！");
            return;
        }
        var arr = [];
        $.each(data, function(key, val) {
            arr[key] = val.id;
        });
        var ids = arr.join(",");
        enterprise_manage.deleteThis(ids)
    },
	/**
	 * 导出选择的数据
	 */
	exportExcel:function(){
		var rows = Table_Obj.getCheckedItem();
		var column = new Array();
		column.push(Table_Obj.getColumns());
		var dataList={};
		dataList.column=column;
		dataList.rows=rows;
		dataList.type="enter";
		if(rows.length == 0){
			layer.alert("未选择导出的数据！");
		}else{
	        var tempForm = document.createElement("form");
	        document.body.appendChild(tempForm);
	        tempForm.method="post";
	        tempForm.action="/crm/exportExcel";
	        var hideInput = document.createElement("input");
	        hideInput.type="hidden";
	        hideInput.name= "datas";
	        hideInput.value= JSON.stringify(dataList);
	        tempForm.appendChild(hideInput);
	        tempForm.submit();
	    }
	},
	/**
	 * 导出查询出的所有数据
	 */
	exportAllToExcel:function(){
		var pageSize = $("#total_records").text();
		var pageIndex = 1;
		var obj = enterprise_manage.getParam();
		obj.pageSize = pageSize;
		obj.pageIndex = pageIndex;
		var column = new Array();
		column.push(Table_Obj.getColumns());
		obj.column=JSON.stringify(column);
		var tempForm = document.createElement("form");
        document.body.appendChild(tempForm);
        tempForm.method="post";
        tempForm.action="/crm/enterprise/exportExcel";
        var hideInput = document.createElement("input");
        hideInput.type="hidden";
        hideInput.name= "datas";
        hideInput.value= JSON.stringify(obj);
        tempForm.appendChild(hideInput);
        tempForm.submit();
	}
};
